<template>
    <div v-if="tkIsShow">
        <div class="zhezhao"></div>
        <div class="tankuang">
            <div class="tk_title">{{ title }}</div>
            <img src="../assets/icon-close.png" class="tk_close" @click="closeTk">
            <div class="tk_content">
                <el-button type="primary" @click="edit(0,'batch')" class="addBTN" size="small" :disabled="batchEditFlag">批量编辑</el-button>
                <el-button type="primary" @click="batchAddSku()" class="addBTN2" size="small">生成规格</el-button>
                <el-table
                        :data="tableData"
                        style="width: 100%;"
                        height="90%"
                        @selection-change="handleSelectionChange">
                        <el-table-column
                        type="selection"
                        width="55">
                        </el-table-column>
                        <el-table-column
                        prop="own_spec"
                        label="规格名称"
                        width="250">
                        </el-table-column>
                        <el-table-column
                        label="规格详细图"
                        width="150">
                        <template slot-scope="scope">
                        <el-image
                            style="width: 100px; height: 100px"
                            :src="scope.row.skuPic"
                            fit="fill" ></el-image>
                        </template>
                        </el-table-column>
                        <el-table-column
                        prop="costPrice"
                        label="成本价"
                        width="120"
                        align="center">
                        </el-table-column>
                        <el-table-column
                        prop="underlinedPrice"
                        label="划线价"
                        width="120"
                        align="center">
                        </el-table-column>
                        <el-table-column
                        prop="salesPrice"
                        label="销售价"
                        width="120"
                        align="center">
                        </el-table-column>
                        <el-table-column
                        prop="stock"
                        label="库存"
                        width="120"
                        align="center">
                        </el-table-column>
                        <el-table-column
                        label="状态"
                        width="120">
                        <template slot-scope="scope">
                            {{ scope.row.enable==1?'有效':'无效' }}
                        </template>
                        </el-table-column>
                        <el-table-column
                        label="操作"
                        width="150">
                        <template slot-scope="scope">
                            <el-button @click="edit(scope.row.skuId,'edit')" type="text" size="medium">编辑</el-button>
                        </template>
                        </el-table-column>
                    </el-table>
                    <pagination
                    :total="total"
                    :limit="pageSize"
                    :page="pageNumber"
                    :pageSize="pageSize"
                    @pageChange="pageChange"
                    ></pagination>
            </div>
        </div>
        <edit-skuBox
        ref="editSkuBox"
        @close="
            getData()
        "
        ></edit-skuBox>
        <add-skuBox
        ref="addSkuBox"
        @close="
            getData()
        "
        ></add-skuBox>
    </div>
</template>
<script>
import editSkuBox from "@/components/editSkuBox.vue";
import addSkuBox from "@/components/addSkuBox.vue"
import Pagination from "@/components/pagination.vue";
export default {
    components: {
    Pagination,
    editSkuBox,
    addSkuBox
  },
  data() {
    return {
        total: 0,
        pageSize: 100,
        pageNumber: 1,
        tkIsShow:false,
        title:"",
        tableData:[],
        spuId:0,
        type:"1",
        editForm:{
            id:0
        },
        batchEditFlag:true,
        selectIds:[]
    };
  },
  methods: {
    __init(id) {
      this.spuId=id;
      this.getData();
    },
    getData(){
        var url="manager/getGoodsSkuList";
        var params={
            id: this.spuId,
            pageNum: this.pageNumber,
            pageSize:this.pageSize
        }
        this.$post(url, params).then((res) => {
            console.log(res)
            if (res.code === 0) {
                this.tableData=res.data.list;
                this.total=res.data.total;
                if(this.total>0){
                    this.batchEditFlag=false;
                }else{
                    this.batchEditFlag=true;
                }
            } else {
                this.$message.error(res.msg)
            }
        });
    },
    pageChange(e) {
      this.pageNumber = e.page;
      this.getData();
    },
    closeTk(){
        this.tkIsShow = false;
    },
    edit(id,str){
        if(str=='batch'){
            //先判断他有没有选择id
            if(this.selectIds.length<=0){
                this.$message.error("请先选择要修改的信息");
                return;
            }
            if (this.$refs.editSkuBox) {
                this.$refs.editSkuBox.tkIsShow = true;
                this.$refs.editSkuBox.title = "批量编辑规格信息";
                this.$refs.editSkuBox.__init("batch",this.selectIds.join(','));
            }
        }else{
            if (this.$refs.editSkuBox) {
                this.$refs.editSkuBox.tkIsShow = true;
                this.$refs.editSkuBox.title = "编辑规格信息";
                this.$refs.editSkuBox.__init("edit",id);
            }
        }
    },
    handleSelectionChange(selection) {
        const skuIds = selection.map(row => row.skuId);
        this.selectIds=skuIds;
    },
    batchAddSku(){
        if (this.$refs.addSkuBox) {
            this.$refs.addSkuBox.tkIsShow = true;
            this.$refs.addSkuBox.title = "生成规格列表";
            this.$refs.addSkuBox.__init(this.spuId);
        }
    }
  }
}
</script>
<style scoped lang="scss">
  .zhezhao{
    position: fixed;
    width: 100%;
    height: 100%;
    background-color: #000;
    opacity: 0.2;
    top: 0;
    left: 0;
    z-index: 100;
  }
  .tankuang{
    position: fixed;
    width: 70%;
    height: 80%;
    background-color: #fff;
    top: 10%;
    left: 15%;
    z-index: 101;
    border-radius: 5px;
  }
  .tk_title{
    font-size: 17px;
    margin: 15px 0;
    height: 25px;
    line-height: 25px;
  }
  .tk_content{
    width: 90%;
    margin-left: 5%;
    height: calc(100% - 55px);
  }
  .tk_close{
    position: absolute;
    width: 16px;
    height: 16px;
    top: 18px;
    right: 20px;
  }
  .addBTN{
    position: absolute;
    top: 10px;
    left: 2%;
  }
  .addBTN2{
    position: absolute;
    top: 10px;
    left: 8%;
  }
  ::v-deep .el-select>.el-input{
    width: 150px !important;
  }
</style>